home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / sdi / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-18  |  7.2 KB  |  267 lines

  1. /************************************  main.c  *************************/
  2. #include "sdi.h"
  3.  
  4. /*
  5.  * Copyright 1987 by Mark Weiser.
  6.  * Permission to reproduce and use in any manner whatsoever on Suns is granted
  7.  * so long as this copyright and other identifying marks of authorship
  8.  * in the code and the game remain intact and visible.  Use of this code
  9.  * in other products is reserved to me--I'm working on Mac and IBM versions.
  10.  */
  11.  
  12. /*
  13.  * A big, long, initialization routine.
  14.  */
  15.  
  16. static short cursor_data[] = {
  17. #include "cursor.h"
  18. };
  19. mpr_static(cursor_pr, 16, 16, 1, cursor_data);
  20.  
  21. static short icon_image[] = {
  22. #include "city_icon1.h"
  23. };
  24. DEFINE_ICON_FROM_IMAGE(icon, icon_image);
  25.  
  26. static short i_pic_array[] = {
  27. #include "incoming_picture.h"
  28. };
  29. mpr_static(incoming_pic, 16, 16, 1, i_pic_array);
  30.  
  31. static short g_pic_array[] = {
  32. #include "foe_ground_picture.h"
  33. };
  34. mpr_static(foe_ground_pic, 16, 16, 1, g_pic_array);
  35.  
  36. static void done_proc();
  37. extern void main_event_proc(), canvas_resize_proc();
  38. extern Notify_value canvas_input_proc(), asynch_event_proc(), synch_event_proc();
  39. Notify_value scheduler(), input_notify();
  40.  
  41. #define FONT_NAME "/usr/lib/fonts/fixedwidthfonts/serif.r.14"
  42.  
  43. static char tenblanks[] = "          ";
  44.  
  45. Panel launchpanel;
  46.  
  47. /*
  48.  * In need of no comment:
  49.  */
  50. main(argc, argv)
  51. char **argv;
  52. {
  53.     extern char *version;
  54.     Menu menu, null_menu_gen();
  55.     char tmp_string[128];
  56.     Panel controlpanel;
  57.     struct timeval tp;
  58.     char *s;
  59.  
  60.     /* randomize us */
  61.     gettimeofday(&tp, 0);
  62.     srandom(tp.tv_sec);
  63.  
  64.     /* init score file from environment, if possible */
  65.     if ((s = (char *)getenv("SDI_SCORES")) != NULL) 
  66.         scorefile = s;
  67.  
  68.     init_circles();
  69.  
  70.     init_city_bits(NULL);
  71.  
  72.     fixup_font(&argc, &argv, FONT_NAME);
  73.  
  74.     open_our_font(FONT_NAME);
  75.     sprintf(tmp_string, "   SDI Control Panel%s%s%s%s%sby mark weiser",
  76.         tenblanks,tenblanks,tenblanks,tenblanks,tenblanks);
  77.  
  78.     s = (char *)get_name();
  79.     if (s && s[0] != '\0')
  80.         strcpy(user_name, s);
  81.  
  82.     /* no background pre-write to our icon--otherwise we flicker. */
  83.     icon.ic_flags = 0;
  84.  
  85.     /* make the control window, which serves as the base for all others. */
  86.     controlframe = window_create(NULL, FRAME,
  87.         FRAME_ARGC_PTR_ARGV, &argc, argv,
  88.         FRAME_LABEL, tmp_string,
  89.         FRAME_ICON, &icon,
  90.         WIN_ERROR_MSG, "Can't create window.",
  91.         WIN_FONT, font,
  92.         WIN_X, 0, WIN_Y, 0, /* start at zero, move later. */
  93.         0);
  94.  
  95.     process_args(argc, argv);
  96.  
  97.     init_icons();
  98.  
  99.     build_playing_fields();
  100.  
  101.     controlpanel = window_create(controlframe, PANEL, 
  102.         WIN_VERTICAL_SCROLLBAR, scrollbar_create(0), /* but provide a loophole */
  103.         PANEL_LABEL_FONT, font,
  104.         PANEL_VALUE_FONT, font,
  105.         WIN_FONT, font,
  106.         WIN_ERROR_MSG, "Can't create window.",
  107.         /* magic numbers which seem to look good: */
  108.         WIN_WIDTH, 730,
  109.         PANEL_ITEM_X_GAP, 30,
  110.         PANEL_ITEM_Y_GAP, 15,
  111.         0);
  112.     init_control(controlpanel);
  113.     window_fit(controlpanel);
  114.     window_fit(controlframe);
  115.  
  116.     city_fd = (int)window_get(citycanvas, WIN_FD);
  117.     launch_fd = (int)window_get(launchcanvas, WIN_FD);
  118.  
  119.     max_x = (int)window_get(citycanvas, CANVAS_WIDTH);
  120.     max_y = (int)window_get(citycanvas, CANVAS_HEIGHT);
  121.  
  122.     { /* little block for a bunch of little variables */
  123.     struct screen screen;
  124.     int playwidth = (int)window_get(cityframe, WIN_WIDTH);
  125.     int controlwidth = (int)window_get(controlframe, WIN_WIDTH);
  126.     int controlheight = (int)window_get(controlframe, WIN_HEIGHT);
  127.     int center;
  128.     win_screenget(city_fd, &screen);
  129.     center =  screen.scr_rect.r_width/2;
  130.  
  131.     /* put the playing frames into position (control is at 0,0) */
  132.     window_set(cityframe, WIN_X, center - playwidth, 0);
  133.     window_set(launchframe, WIN_X, center, 0);
  134.  
  135.     /* center the control frame */
  136.     window_set(controlframe, WIN_X, center - controlwidth/2, 
  137.         /* magic number which seems to look good: */
  138.         WIN_Y, 200, 0);
  139.  
  140.     /* put the playing frames below it. */
  141.     window_set(cityframe, WIN_Y, controlheight, 0);
  142.     window_set(launchframe, WIN_Y, controlheight, 0);
  143.     } /* end of little block */
  144.  
  145.     /*
  146.      * menus in the playing fields are a distraction, but
  147.      * they can't be just menu-destroyed, because that leaves a
  148.      * dangling pointer inside sunview (and a core dump). 
  149.      * So instead, we cleverly(?) modify the menu so that it will 
  150.      * generate an empty contents.
  151.      * (Ychhhh!)
  152.      */
  153.     menu = window_get(launchframe, WIN_MENU);
  154.     menu_set(menu, MENU_GEN_PROC, null_menu_gen, 0);
  155.     menu = window_get(cityframe, WIN_MENU);
  156.     menu_set(menu, MENU_GEN_PROC, null_menu_gen, 0);
  157.  
  158.     notify_interpose_event_func(launchframe, synch_event_proc, NOTIFY_SAFE);
  159.     notify_interpose_event_func(cityframe, synch_event_proc, NOTIFY_SAFE);
  160.     notify_interpose_event_func(controlframe, synch_event_proc, NOTIFY_SAFE);
  161.  
  162.     notify_set_scheduler_func(scheduler);
  163.  
  164.     notify_set_input_func(citycanvas, input_notify, city_fd);
  165.     notify_set_input_func(launchcanvas, input_notify, launch_fd);
  166.  
  167.     init_cursor();
  168.     update_cursor();
  169.  
  170.     window_main_loop(controlframe);
  171.     exit(0);
  172. }
  173.  
  174. static void
  175. done_proc(frame)
  176. Frame frame;
  177. {
  178.     /* do nothing */
  179. }
  180.  
  181. build_playing_fields()
  182. {
  183.     Panel citypanel;
  184.     extern Panel_item foe_ground_item;
  185.  
  186.     cityframe = window_create(controlframe, FRAME,
  187.         FRAME_LABEL, "   SDI Friend Cities",
  188.         FRAME_SHOW_LABEL, TRUE,
  189.         FRAME_DONE_PROC, done_proc,
  190.         WIN_ERROR_MSG, "Can't create window.",
  191.         WIN_FONT, font,
  192.         FRAME_SUBWINDOWS_ADJUSTABLE, FALSE,
  193.         0);
  194.  
  195.     citypanel = window_create(cityframe, PANEL, 0);
  196.     ballistic_item = panel_create_item(citypanel, PANEL_SLIDER,
  197.         ATTR_LIST, panel_common,
  198.         PANEL_LABEL_STRING, "Incoming missiles: ",
  199.         0);
  200.     window_fit_height(citypanel);
  201.  
  202.     citycanvas = window_create(cityframe, CANVAS,
  203.         WIN_CONSUME_PICK_EVENTS, WIN_UP_EVENTS,  0,
  204.         WIN_EVENT_PROC, main_event_proc,
  205.         WIN_CURSOR, cursor_create(CURSOR_IMAGE, &cursor_pr,
  206.             CURSOR_XHOT, 8, CURSOR_YHOT, 8,
  207.             CURSOR_OP, PIX_SRC ^ PIX_DST,
  208.             0),
  209.         WIN_WIDTH, max_x+(2*FIELD_MARGIN), WIN_HEIGHT, max_y+(2*FIELD_MARGIN),
  210.         CANVAS_RESIZE_PROC, canvas_resize_proc,
  211.         CANVAS_RETAINED, TRUE,    /* need retained for city computations */
  212.         WIN_ERROR_MSG, "Can't create window.",
  213.         CANVAS_MARGIN, 0,
  214.         0);
  215.     window_fit(cityframe);
  216.     citypw = pw_region(canvas_pixwin(citycanvas), FIELD_MARGIN, FIELD_MARGIN, 
  217.         max_x, max_y);
  218.  
  219.     launchframe = window_create(controlframe, FRAME,
  220.         FRAME_LABEL, "   SDI Foe Launch",
  221.         FRAME_SHOW_LABEL, TRUE,
  222.         FRAME_DONE_PROC, done_proc,
  223.         WIN_ERROR_MSG, "Can't create window.",
  224.         WIN_FONT, font,
  225.         FRAME_SUBWINDOWS_ADJUSTABLE, FALSE,
  226.         0);
  227.  
  228.     launchpanel = window_create(launchframe, PANEL, 0);
  229.     foe_ground_item = panel_create_item(launchpanel, PANEL_SLIDER,
  230.         ATTR_LIST, panel_common,
  231.         PANEL_LABEL_STRING, "Missiles on the ground:", 
  232.         0);
  233.     window_fit_height(launchpanel);
  234.     launchcanvas = window_create(launchframe, CANVAS,
  235.         WIN_CONSUME_PICK_EVENTS, WIN_UP_EVENTS,  0,
  236.         WIN_EVENT_PROC, main_event_proc,
  237.         WIN_CURSOR, cursor_create(CURSOR_IMAGE, &cursor_pr,
  238.             CURSOR_XHOT, 8, CURSOR_YHOT, 8,
  239.             CURSOR_OP, PIX_SRC ^ PIX_DST,
  240.             0),
  241.         WIN_WIDTH, max_x+(2*FIELD_MARGIN), WIN_HEIGHT, max_y+(2*FIELD_MARGIN),
  242.         CANVAS_RESIZE_PROC, canvas_resize_proc,
  243.         CANVAS_RETAINED, TRUE,
  244.         WIN_ERROR_MSG, "Can't create window.",
  245.         CANVAS_MARGIN, 0,
  246.         0);
  247.  
  248.     window_fit(launchframe);
  249.  
  250.     launchpw = pw_region(canvas_pixwin(launchcanvas), FIELD_MARGIN, FIELD_MARGIN,
  251.         max_x, max_y);
  252.  
  253. /*
  254.  *    A noble sentiment, but too much trouble to handle resizing correctly.
  255.     window_set(launchcanvas, 
  256.         WIN_HEIGHT, window_get(launchcanvas, WIN_HEIGHT),
  257.         WIN_Y, 0,
  258.          0);
  259.     window_set(launchpanel, WIN_BELOW, launchcanvas, 
  260.         0);
  261.  
  262. */
  263.  
  264.     draw_background();
  265.  
  266. }
  267.